package com.wb.business.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.project.relation.domain.ao.SyncActionRequest;
import com.ruoyi.project.relation.domain.ao.SyncActonResponse;
import com.ruoyi.project.relation.mapper.GlobalMapper;
import com.ruoyi.project.relation.mapper.domain.GlobalUpdateBitColumnMO;
import com.ruoyi.project.relation.mapper.domain.GlobalUpdateMultipleBitColumn;
import com.wb.business.service.BusinessEntityService;
import com.wb.business.utils.SyncActionUtils;
import org.shoukaiseki.aqoeous.config.Validator;
import org.shoukaiseki.common.utils.AssertMessageVirtualException;
import org.shoukaiseki.common.utils.MybatisUtils;
import org.shoukaiseki.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GlobalService  implements BusinessEntityService {

    @Autowired
    private GlobalMapper globalMapper;

    private BusinessEntityManager businessEntityManager;

    @Resource
    private Validator validator;

    @Override
    public void setBusinessEntityManager(BusinessEntityManager businessEntityManager) {
        this.businessEntityManager = businessEntityManager;
    }

    //启动不了
//    @Autowired
//    private BindingResult bindingResult;

    @Autowired
    public void setGlobalMapper(GlobalMapper globalMapper){
        this.globalMapper=globalMapper;
    }


    @Transactional
    public int updateBitColumn(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }



    @Transactional
    public int updatePaymentStatus(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        globalUpdateBitColumnMO.setAttrName("payment_status");
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }
    @Transactional
    public int updateWorkStatus(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        globalUpdateBitColumnMO.setAttrName("work_status");
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }


    @Transactional
    public int updateProcessStatus(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        globalUpdateBitColumnMO.setAttrName("process_status");
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }

    @Transactional
    public int updateBitFlag(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        globalUpdateBitColumnMO.setAttrName("bit_flag");
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }

    @Transactional
    public int updateBitStatus(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        globalUpdateBitColumnMO.setAttrName("bit_status");
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }

    @Transactional
    public int updateStatus(GlobalUpdateBitColumnMO globalUpdateBitColumnMO){
        globalUpdateBitColumnMO.setAttrName("status");
        return globalMapper.updateBitColumn(globalUpdateBitColumnMO);
    }

    @Override
    public Object getBusinessEntityServiceImpl() {
        return this;
    }

    @Override
    public String getTableName() {
        //只为注册个服务名,无该字段
        return "sksGlobal";
    }

    public static final String UPDATE_MULTIPLE_BIT_COLUMN="updateMultipleBitColumn";

    @Override
    public SyncActonResponse syncAction(SyncActionRequest syncActionRequest) {
        SyncActonResponse syncActonResponse = new SyncActonResponse();

        if (UPDATE_MULTIPLE_BIT_COLUMN.equals(syncActionRequest.getBeanName())) {

            GlobalUpdateMultipleBitColumn globalUpdateMultipleBitColumn = SyncActionUtils.domainToJavaBean(syncActionRequest.getDomain(), GlobalUpdateMultipleBitColumn.class);
            List<GlobalUpdateBitColumnMO> globalUpdateBitColumnMOList = globalUpdateMultipleBitColumn.getGlobalUpdateBitColumnMOList();
            String tableName = globalUpdateMultipleBitColumn.getTableName();
            MybatisUtils.isValidTableOrColumnSql(tableName);
            BusinessEntityService businessEntityService = businessEntityManager.getBusinessEntityService(tableName);
            String uniqueIdName = businessEntityService.getUniqueIdName();
            globalUpdateMultipleBitColumn.setTableIdName(uniqueIdName);
//            validator.validate(globalUpdateMultipleBitColumn,bindingResult);
//            AssertMessageVirtualException.anyNullThrow("任何一个不能为空",tableName,uniqueIdName);

            for (GlobalUpdateBitColumnMO globalUpdateBitColumnMO : globalUpdateBitColumnMOList) {
//                validator.validate(globalUpdateBitColumnMO,bindingResult);

                String attrName = globalUpdateBitColumnMO.getAttrName();
                String tableIdName = globalUpdateBitColumnMO.getTableIdName();
                MybatisUtils.isValidTableOrColumnSql(attrName);
                MybatisUtils.isValidTableOrColumnSql(tableIdName);
            }
            int i = globalMapper.updateMultipleBitColumn(globalUpdateMultipleBitColumn);
            syncActonResponse.setData(i);
            return syncActonResponse;

        }


        return BusinessEntityService.super.syncAction(syncActionRequest);
    }
}
